<?php

function b_mylinks_top_show($options) {
    global $xoopsDB;

       $mydirname = empty( $options[3] ) ? "mylinks" : $options[3] ;
         if($options[0] == "date"){
       $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_new.html' : trim( $options[4] ) ;
       }
       else{
       $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_top.html' : trim( $options[4] ) ;
       }
       if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;


    $block = array();
    //ver2.5
    $block['mylinks_weburl'] = XOOPS_URL . '/modules/' . $mydirname;

    $myts =& MyTextSanitizer::getInstance();
    $result = $xoopsDB->query("SELECT lid, cid, title, date, hits FROM ".$xoopsDB->prefix($mydirname."_links")." WHERE status>0 ORDER BY ".$options[0]." DESC",$options[1],0);
    while($myrow = $xoopsDB->fetchArray($result)){
        $link = array();
        $title = $myts->makeTboxData4Show($myrow["title"]);
        if ( !XOOPS_USE_MULTIBYTES ) {
            if (strlen($myrow['title']) >= $options[2]) {
                $title = $myts->makeTboxData4Show(substr($myrow['title'],0,($options[2] -1)))."...";
            }
        }
        $link['id'] = $myrow['lid'];
        $link['cid'] = $myrow['cid'];
        $link['title'] = $title;
        if($options[0] == "date"){
            $link['date'] = formatTimestamp($myrow['date'],'s');
        }elseif($options[0] == "hits"){
            $link['hits'] = $myrow['hits'];
        }
        $block['links'][] = $link;
    }

	if( empty( $options['disable_renderer'] ) ) {
		require_once XOOPS_ROOT_PATH.'/class/template.php' ;
		$tpl =& new XoopsTpl() ;
		$tpl->assign( 'block' , $block ) ;
		$ret['content'] = $tpl->fetch( $this_template ) ;
		return $ret ;
	} else {
		return $block ;
	}

}

function b_mylinks_top_edit($options) {

    $mydirname = empty( $options[3] ) ? "mylinks" : $options[3] ;
       if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

    if($options[0] == "date"){
    $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_new.html' : trim( $options[4] ) ;
    }
    else{
    $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_top.html' : trim( $options[4] ) ;
    }

    $form = ""._MB_MYLINKS_DISP."&nbsp;";
    $form .= "<input type='hidden' name='options[]' value='";
    if($options[0] == "date"){
        $form .= "date'";
    }else {
        $form .= "hits'";
    }
    $form .= " />";
    $form .= "<input type='text' name='options[]' value='".$options[1]."' />&nbsp;"._MB_MYLINKS_LINKS."";
    $form .= "&nbsp;<br />"._MB_MYLINKS_CHARS."&nbsp;<input type='text' name='options[]' value='".$options[2]."' />&nbsp;"._MB_MYLINKS_LENGTH."";
    $form .= "<input type='hidden' name='options[]' value='".$mydirname."' />";
    $form .= "<input type='hidden' name='options[]' value='".htmlspecialchars($this_template,ENT_QUOTES)."' />";

    return $form;
}

?>